﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Webdisk;

namespace Webdisk_New
{
	public partial class Encol : Form
	{
		public Encol()
		{
			InitializeComponent();
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);

        }

        private void button1_Click(object sender, EventArgs e)
		{

			string ConnString = "server = 134.175.15.205;database = Webdisk;uid=sa;pwd=WHXL_whxl_1024";
			//创建连接对象			
			string sqlselect = "select count(*) from userInfo where LoginName='" + loginName_TextBox.Text + "'";
			SqlConnection con = new SqlConnection(ConnString);
			SqlCommand cmd = new SqlCommand(sqlselect, con);
			con.Open();
			if (loginName_TextBox.Text == "" || loginPass_TextBox.Text == "")
			{
				MessageBox.Show("账号和密码不能为空，请重新输入", "输入提示");
			}
			else
			{
				if ((int)cmd.ExecuteScalar() > 0)
				{
					con.Close();
					MessageBox.Show("用户已存在！");
				}
				else if (loginPass_TextBox.Text == loginPass_Again_TextBox.Text)
				{
					DBHelper.ExecuteNonQuery(string.Format("insert into userInfo(loginName,loginPass,StartTime) values('{0}','{1}',GETDATE())", loginName_TextBox.Text, loginPass_TextBox.Text));
					MessageBox.Show("添加成功", "温馨提示");
					this.Close();
					log_on_Form log = new log_on_Form();
					log.Show();
				}
				else
				{
					MessageBox.Show("密码错误，请重新输入！");
				}
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			loginName_TextBox.Clear();
			loginPass_TextBox.Clear();
			loginPass_Again_TextBox.Clear();
		}

		private void Encol_FormClosing(object sender, FormClosingEventArgs e)
		{

			if (e.Cancel == false)
			{
				log_on_Form log = new log_on_Form();
				log.Show();
			}
            Type.AnimateWindow(this.Handle, 300, Type.AW_BLEND | Type.AW_HIDE);
        }

        //设置窗口外阴影
        private const int CS_DropSHADOW = 0x20000;
        private const int GCL_STYLE = (-26);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        




        private void Encol_Load(object sender, EventArgs e)
        {
            buttonReg.Visible = false;
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }
        private Point mPoint;//坐标
        //下面两个事件实现了窗口的拖动
        private void Encol_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Encol_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }


        Boolean textboxHasTextName = false;//判断输入框是否有文本：用户名框       
        Boolean textboxHasTextPass = false;//判断输入框是否有文本：密码框
        Boolean textboxHascfmpass = false;//确认密码
        private void loginName_TextBox_Enter_1(object sender, EventArgs e)
        {
            if (textboxHasTextName == false)
            {
                loginName_TextBox.Text = "";
            }
           loginName_TextBox.ForeColor = Color.Black;//当前控件的前景色等于黑色
        }

        private void loginName_TextBox_Leave(object sender, EventArgs e)
        {
            if (loginName_TextBox.Text == "")
            {
                loginName_TextBox.Text = "用户名";
                loginName_TextBox.ForeColor = Color.Silver;//提示文本的颜色为灰色
                textboxHasTextName = false;
            }
            else
            {
                textboxHasTextName = true;
            }
        }
     
        /// ////////////////////////////////////////////////////////////////////////////密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginPass_TextBox_Enter_1(object sender, EventArgs e)
        {
            if (textboxHasTextPass == false)
            {
                loginPass_TextBox.Text = "";

            }

            loginPass_TextBox.ForeColor = Color.Black;//当前控件的前景色等于黑色
        }
        private void loginPass_TextBox_Leave(object sender, EventArgs e)
        {
            if (loginPass_TextBox.Text == "")
            {
                loginPass_TextBox.Text = "密码";
                loginPass_TextBox.ForeColor = Color.Silver;//提示文本的颜色为灰色
                textboxHasTextPass = false;
            }
            else
            {
                textboxHasTextPass = true;
            }
        }
        private void loginPass_TextBox_MouseEnter_1(object sender, EventArgs e)
        {

            if (textboxHasTextPass == false)
            {
                loginPass_TextBox.Text = "";

            }

            loginPass_TextBox.ForeColor = Color.Black;//当前控件的前景色等于黑色
        }

        private void loginPass_TextBox_MouseLeave_1(object sender, EventArgs e)
        {

            if (loginPass_TextBox.Text == "")
            {
                loginPass_TextBox.Text = "密码";
                loginPass_TextBox.ForeColor = Color.Silver;//提示文本的颜色为灰色
                textboxHasTextPass = false;
            }
            else
            {
                textboxHasTextPass = true;
            }
        }

        private void loginPass_TextBox_TextChanged_1(object sender, EventArgs e)
        {
            if (loginPass_TextBox.Text != "" && loginPass_TextBox.Text != "密码")
            {
                //将输入的字符转换为*
                loginPass_TextBox.PasswordChar = '*';
            }
            else
            {
                //将字符设为提示文本
                loginPass_TextBox.PasswordChar = new char();
            }
        }

        /// <summary>
        /// /////////////////////////////////////////////////////////////确认密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfirmPass_textBox_Enter(object sender, EventArgs e)
        {
            if (textboxHascfmpass == false)
            {
                loginPass_Again_TextBox.Text = "";

            }

            loginPass_Again_TextBox.ForeColor = Color.Black;//当前控件的前景色等于黑色
        }
        private void ConfirmPass_textBox_Leave(object sender, EventArgs e)
        {
            if (loginPass_Again_TextBox.Text == "")
            {
                loginPass_Again_TextBox.Text = "确认密码";
                loginPass_Again_TextBox.ForeColor = Color.Silver;//提示文本的颜色为灰色
                textboxHascfmpass = false;
            }
            else
            {
                textboxHascfmpass = true;
            }
        }

        private void ConfirmPass_textBox_TextChanged(object sender, EventArgs e)
        {
            if (loginPass_Again_TextBox.Text != "" && loginPass_Again_TextBox.Text != "确认密码")
            {
                //将输入的字符转换为*
                loginPass_Again_TextBox.PasswordChar = '*';
            }
            else
            {
                //将字符设为提示文本
                loginPass_Again_TextBox.PasswordChar = new char();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Close();
        }
        //鼠标经过
        private void pictureBoxMin_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxMin.Image = imageList.Images[1];
        }
        //鼠标经过
        private void pictureBoxclose_MouseEnter(object sender, EventArgs e)
        {
            pictureBoxclose.Image = imageList.Images[0];
        }
        //鼠标经过
        private void pictureBox_Register_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_Register.Image = Image.FromFile("Pic\\立即注册经过.png");

        }
        //鼠标离开
        private void pictureBoxMin_MouseLeave(object sender, EventArgs e)
        {
            pictureBoxMin.Image = null;
        }
        //鼠标离开
        private void pictureBoxclose_MouseLeave(object sender, EventArgs e)
        {
            pictureBoxclose.Image = null;
        }
        //鼠标离开
        private void pictureBox_Register_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_Register.Image = Image.FromFile("Pic\\立即注册按下.png");

        }
        //最小化窗体
        private void pictureBoxMin_MouseClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        //关闭窗体
        private void pictureBoxclose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBox_Register_Click(object sender, EventArgs e)
        {
            if (loginName_TextBox.Text == "" || loginName_TextBox.Text == "用户名" && loginName_TextBox.ForeColor == Color.SlateGray)
            {
                MessageBox.Show("请输入用户名");
                IdentifyingCode(num1);
                IdentifyingCode(num2);
                IdentifyingCode(num3);
                IdentifyingCode(num4);
            }
            else if (loginPass_TextBox.Text == "" || loginPass_TextBox.Text == "密码" && loginPass_TextBox.ForeColor == Color.SlateGray)
            {
                MessageBox.Show("请输入密码");
                IdentifyingCode(num1);
                IdentifyingCode(num2);
                IdentifyingCode(num3);
                IdentifyingCode(num4);
            }
            else if (loginPass_Again_TextBox.Text == "" || loginPass_Again_TextBox.Text == "确认密码" && loginPass_Again_TextBox.ForeColor == Color.SlateGray)
            {
                MessageBox.Show("请再次输入密码");
                IdentifyingCode(num1);
                IdentifyingCode(num2);
                IdentifyingCode(num3);
                IdentifyingCode(num4);
            }
            else if (loginPass_Again_TextBox.Text != loginPass_TextBox.Text)
            {
                MessageBox.Show("两次密码不一致");
                IdentifyingCode(num1);
                IdentifyingCode(num2);
                IdentifyingCode(num3);
                IdentifyingCode(num4);
            } else if (Code.Text!=num1.Text+num2.Text+num3.Text+num4.Text)
            {
                MessageBox.Show("验证码错误");
                IdentifyingCode(num1);
                IdentifyingCode(num2);
                IdentifyingCode(num3);
                IdentifyingCode(num4);
            }
            else
            {
                bool same = false;//是否找到相同
                DBHelper.InitConnection();
                DBHelper.Conn.Close();
                DBHelper.Conn.Open();

                SqlDataReader reader = DBHelper.GetDataReader("select * from userInfo");
                while (reader.Read())
                {
                    string loginName = reader["loginName"].ToString();
                    if (loginName_TextBox.Text == loginName)
                    {
                        MessageBox.Show("用户名已存在");
                        IdentifyingCode(num1);
                        IdentifyingCode(num2);
                        IdentifyingCode(num3);
                        IdentifyingCode(num4);
                        same = true;
                        break;
                    }
                }

                if (same == false)
                {
                    reader.Close();

                    bool a = DBHelper.ExecuteNonQuery(string.Format("insert into userInfo(loginName,loginPass,StartTime)values('{0}','{1}',GETDATE())", loginName_TextBox.Text, loginPass_TextBox.Text));

                    if (a == true)
                    {
                        MessageBox.Show("注册成功");
                        Temp.Folder = false;


                    }
                    else
                    {

                        MessageBox.Show("注册失败");
                        IdentifyingCode(num1);
                        IdentifyingCode(num2);
                        IdentifyingCode(num3);
                        IdentifyingCode(num4);
                    }

                }

                DBHelper.Conn.Close();


            }
        }



        Random FontType = new Random();//全局随机数


        private void IdentifyingCode(Label label)//验证码
        {
            int num = FontType.Next(0, 10);//随机数字
            int Size = FontType.Next(22, 25);//随机大小
            int Type = FontType.Next(0, 12);//随机样式（下划线啊什么鬼的）
            int FontFa = FontType.Next(0, 6);//随机字体（微软雅黑啊什么鬼的）
            Random rand1 = new Random();
            Color Col = Color.FromArgb(rand1.Next(256), rand1.Next(256), rand1.Next(256));//随机颜色
            label.Text = num + "";
            label.ForeColor = Col;//字体颜色
            float size = label.Font.Size;//字体大小
            FontFamily myFontFamily = new FontFamily("微软雅黑"); //采用哪种字体
            try
            {
                if (FontFa == 0)
                {
                    myFontFamily = new FontFamily("Comic Sans MS"); //采用哪种字体

                }
                else if (FontFa == 1)
                {
                    myFontFamily = new FontFamily("宋体"); //采用哪种字体

                }
                else if (FontFa == 2)
                {
                    myFontFamily = new FontFamily("Gabriola"); //采用哪种字体

                }
                else if (FontFa == 3)
                {
                    myFontFamily = new FontFamily("Gabriola"); //采用哪种字体

                }
                else if (FontFa == 4)
                {
                    myFontFamily = new FontFamily("仿宋"); //采用哪种字体

                }
                else if (FontFa == 5)
                {
                    myFontFamily = new FontFamily("黑体"); //采用哪种字体

                }
                else if (FontFa == 5)
                {
                    myFontFamily = new FontFamily("楷体"); //采用哪种字
                }
            }
            catch
            {
                return;
            }

            if (Type == 0)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Italic);//普通倾斜
            }
            else if (Type == 1)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Underline);//普通下划线
            }
            else if (Type == 2)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Strikeout);//普通中线
            }
            else if (Type == 3)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Strikeout);//普通中线
            }
            else if (Type == 4)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Regular);//普通
            }
            else if (Type == 5)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Bold);//加粗
            }
            else if (Type == 6)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Bold | FontStyle.Italic);//加粗+倾斜
            }
            else if (Type == 7)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Bold | FontStyle.Underline);//加粗+下划线
            }
            else if (Type == 8)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Bold | FontStyle.Strikeout);//加粗+中线
            }
            else if (Type == 9)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Bold | FontStyle.Strikeout | FontStyle.Underline);//加粗+中线+下划线
            }
            else if (Type == 10)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Regular | FontStyle.Strikeout | FontStyle.Underline);//普通+中线+下划线
            }
            else if (Type == 11)
            {
                label.Font = new Font(myFontFamily, Size, FontStyle.Italic | FontStyle.Strikeout | FontStyle.Underline);//倾斜+中线+下划线
            }



        }





        private void IdentifyingCode_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        private void num1_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        private void num2_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        private void num3_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        private void num4_Click(object sender, EventArgs e)
        {
            IdentifyingCode(num1);
            IdentifyingCode(num2);
            IdentifyingCode(num3);
            IdentifyingCode(num4);
        }

        private void pictureBox_Register_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox_Register.Image = Image.FromFile("Pic\\立即注册按下.png");
        }

        private void pictureBox_Register_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox_Register.Image = Image.FromFile("Pic\\立即注册经过.png");

        }


        //用户点击
        private void loginName_TextBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (loginName_TextBox.Text=="用户名")
            {
                loginName_TextBox.Text = null;
                loginName_TextBox.ForeColor = Color.Black;
            }           
        }
        //用户离开
        private void loginName_TextBox_MouseLeave(object sender, EventArgs e)
        {
            if (loginName_TextBox.Text == null)
            {
                loginName_TextBox.Text = "用户名";
                loginName_TextBox.ForeColor = Color.Gray;
            }
        }
        //密码点击
        private void loginPass_TextBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (loginPass_TextBox.Text=="密码")
            {
                loginPass_TextBox.Text = null;
                loginPass_TextBox.ForeColor = Color.Black;
            }
            
        }
        //密码离开
        private void loginPass_TextBox_MouseLeave(object sender, EventArgs e)
        {
            if (loginPass_TextBox.Text==null)
            {
                loginPass_TextBox.Text = "密码";
                loginPass_TextBox.ForeColor = Color.Gray;
            }
        }
        //确认密码点击
        private void loginPass_Again_TextBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (loginPass_Again_TextBox.Text=="确认密码")
            {
                loginPass_Again_TextBox.Text = null;
                loginPass_Again_TextBox.ForeColor = Color.Black;
            }
        }
        //确认密码离开
        private void loginPass_Again_TextBox_MouseLeave(object sender, EventArgs e)
        {
            if (loginPass_Again_TextBox.Text == null)
            {
                loginPass_Again_TextBox.Text = "确认密码";
                loginPass_Again_TextBox.ForeColor = Color.Gray;
            }
        }
        //验证码点击
        private void Code_MouseClick(object sender, MouseEventArgs e)
        {
            if (Code.Text=="验证码")
            {
                Code.Text = null;
                Code.ForeColor = Color.Black;
            }
        }
        //验证码离开
        private void Code_MouseLeave(object sender, EventArgs e)
        {
            if (Code.Text==null)
            {
                Code.Text = "验证码";
                Code.ForeColor = Color.Gray;
            }
        }
    }
}
